package com.wuyan.locationdog.ui.device

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_dog_file_info
import com.arcsoft.commonbase.ext.btnEnable
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.gone
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.DeviceFilesListAdapter
import com.wuyan.locationdog.databinding.DogFilesListActivityBinding
import com.wuyan.locationdog.help.DogHelp
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class DogFilesListActivity : BaseBindingActivity<DogFilesListActivityBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { DeviceFilesListAdapter(R.layout.item_device_files_list, null) }

    override fun initData() {


        binding.apply {
            if (DogHelp.isGoDetail) {
                setTitleText("猎犬档案")
                btnRight.gone()
                btnSure.gone()
            } else {
                setTitleText("绑定猎犬")
                btnAdd.gone()
                btnEnable(btnSure, false)

            }
            btnRight.setOnClickListener {
                btnAdd.callOnClick()
            }
            btnAdd.setOnClickListener {
                ActivityUtils.startActivity(DogAddFilesActivity::class.java)
            }

            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无猎犬档案", com.arcsoft.commonbase.R.mipmap.empty_default, "添加猎犬后，将为您管理", "立即添加") {
                        ActivityUtils.startActivity(DogAddFilesActivity::class.java)

                    })

                    setOnItemClickListener { adapter, view, position ->
                        if (DogHelp.isGoDetail) {
                            DogHelp.mDogFileBeanContent = data[position]
                            ActivityUtils.startActivity(DogFilesDetailActivity::class.java)
                        } else {
                            data.find { it.isSelect }?.isSelect = false
                            data[position].isSelect = true
                            notifyDataSetChanged()
                            btnSure.isEnabled = true
                            btnEnable(btnSure, true)

                        }

                    }
                }

                btnSure.setOnClickListener {
                    Bus.post(bus_dog_file_info, mAdapter.data.find { it.isSelect })
                    finish()
                }
            }


        }
    }

    override fun onResume() {
        super.onResume()
        loadNetData()
    }

    override fun loadNetData() {
        super.loadNetData()

        val map = HashMap<String, Any>()
        map["pageSize"] = 9999
        map["pageNum"] = 1
        if (!DogHelp.isGoDetail) {
            map["ifBindDevice"] = false
        }

        mViewModel.getDogList(map)
    }

    override fun observe() {
        super.observe()

        mViewModel.mDogFileBean.observe(this) {
            mAdapter.setNewInstance(it.content.toMutableList())

        }

    }

}